package com.ymatou.iapi.rule.testcase;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFeeTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFreightTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.rule.parameter.ComputeBean;
import com.ymatou.iapi.rule.parameter.RuleComputeResp;
import com.ymatou.iapi.rule.service.ComputeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sqlwapper.orderrisk.OrderRiskWapper;
import com.ymttest.utils.TestDataManager;
/**
*计算规则
*FreeMaker Template自动生成代码
*
*mysql.shop2cn.com - ruleengine
*查询规则sql
SELECT 
        a.RuleId as '规则编号',
        a.RulePriority as '优先级',
        d.ItemText as '规则类型',
        a.RuleTip as '规则信息',
        a.Description as '规则描述',
        c.ItemText as '业务类型',
        b.RuleItemId as '规则项编号',
        b.RuleItemName as '规则项',
        e.ItemText as '规则项名称',
        f.ItemText as '比较符',
        b.RuleItemValue as '规则值',
        g.ItemText as '规则值类型'
FROM rule a
LEFT JOIN rule_item b on a.RuleId = b.RuleId
LEFT JOIN rule_dict_item c on c.ItemValue = a.BizType AND c.DictId = 1
LEFT JOIN rule_dict_item d on d.ItemValue = a.RuleType AND d.DictId = 2
LEFT JOIN rule_dict_item e on e.ItemValue = b.RuleItemName AND e.DictId = 3
LEFT JOIN rule_dict_item f on f.ItemValue = b.ComputeType AND f.DictId = 4
LEFT JOIN rule_dict_item g on g.ItemValue = b.RuleItemType AND g.DictId = 5
WHERE a.BizType = 11 
ORDER BY a.RuleId,c.ItemText;



SELECT a.DictName '字典类型',b.ItemText '字典名称',b.ItemValue '字典值' FROM rule_dict a
JOIN rule_dict_item b on a.Id = b.DictId
#WHERE a.DictName = '业务类型' AND a.Enabled = 1
ORDER BY a.DictName,b.SortNo

*/
@TestSuite
public class Ts_Compute{
	private static ComputeBean computeBean;
	private static ComputeCall computeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("计算规则");
	}

	@Before
	public void caseUp() {
		computeBean=new ComputeBean();
		computeCall=new ComputeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(computeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	public void setHighRiskProduct(String productid) {
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Compute_001() {
		Logger.start(true,"计算规则单规格高风险商品购买数量大于等于3风控-高风险商品");
		try {
			
			PlaceOrderReq placeOrderReq = TradingCallService.buildDemoReq(false);
			
			OrderVo orderVo = placeOrderReq.getOrderGroups().get(0).getOrders()
					.get(0);
			OrderItemVo orderItemVo = orderVo.getOrderItems().get(0);
			orderItemVo.setFreight(new BigDecimal(3));
			
			Logger.comment("############################下单-支付-卖家接单-鉴定中心发货-确认收货");
			//下单-支付-卖家接单-鉴定中心发货-确认收货
            TradingServiceResp placeOrderResp = TradingCallService.callPlaceOrderService(placeOrderReq);

            String payType = "13";

            // 支付收单
            Long thirdTradingId = TradingCallService.payOrder(placeOrderResp.getUserId(), BigDecimal.ZERO,
                            placeOrderResp.getTradingId(), payType);

            // 支付网关支付回调交易(付款)
            PayGateWayCallService.notifyCall(payType, String.valueOf(thirdTradingId),
                            placeOrderResp.getPayableAmount().toString(), placeOrderResp.getUserId());
            Long orderid = placeOrderResp.getOrderList().get(0).getOrderId();
            String pid=placeOrderReq.getOrderGroups().get(0).getOrders().get(0).getOrderItems().get(0).getProductId();
			//SELECT * FROM `orderrisk`.high_risk_seller limit 10;
			//SELECT * FROM `orderrisk`.high_risk_product limit 10;
            
            OrderRiskWapper DefWapper=new OrderRiskWapper();
    		DefWapper.insertHighRiskProduct(pid);
//    		System.out.println(DefWapper.deleteHighRiskProduct("pid1"));
            
			computeBean.setBizNo(TestDataManager.getRandomUUID().toString());
			computeBean.setBizType(11);
			Map<String, String> fact=new HashedMap();
			fact.put("orderid", orderid.toString());
			computeBean.setFact(fact);
			computeCall.setData(computeBean);
			computeCall.callService();			
			Logger.verifyEquals("true", computeCall.getString("success"), "验证success");
//			DefWapper.deleteHighRiskProduct(pid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_002() {
		Logger.start(true,"计算规则单规格高风险商品购买数量小于3不风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_003() {
		Logger.start(true,"单规格高价商品购买数量大于等于3风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_004() {
		Logger.start(true,"单规格低价商品购买数量大于等于10风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_005() {
		Logger.start(true,"多规格高风险买手购买数量大于等于5风控-高风险买手");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_006() {
		Logger.start(true,"多规格高风险买手购买数量小于5不风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_007() {
		Logger.start(true,"多规格高风险商品累计购买数量大于等于3风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_008() {
		Logger.start(true,"多规格高价商品购买数量大于等于3风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_009() {
		Logger.start(true,"多规格低价商品购买数量大于等于10风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_010() {
		Logger.start(true,"用户一天内支付订单数量>=10风控");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_011() {
		Logger.start(true,"用户一周内支付订单数量>=20风控-修改订单时间");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			computeCall.setData(computeBean);
			computeCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_012() {
		Logger.start(true,"同一店铺一天内新用户下单数量>=20风控");
		try {
			PlaceOrderReq placeOrderReq = TradingCallService.buildDemoReq(false);

			OrderVo orderVo = placeOrderReq.getOrderGroups().get(0).getOrders()
					.get(0);
			OrderItemVo orderItemVo = orderVo.getOrderItems().get(0);
			orderItemVo.setFreight(new BigDecimal(3));
			
			Logger.comment("############################下单-支付-卖家接单-鉴定中心发货-确认收货");
			//下单-支付-卖家接单-鉴定中心发货-确认收货
            TradingServiceResp placeOrderResp = TradingCallService.callPlaceOrderService(placeOrderReq);

            String payType = "13";

            // 支付收单
            Long thirdTradingId = TradingCallService.payOrder(placeOrderResp.getUserId(), BigDecimal.ZERO,
                            placeOrderResp.getTradingId(), payType);

            // 支付网关支付回调交易(付款)
            PayGateWayCallService.notifyCall(payType, String.valueOf(thirdTradingId),
                            placeOrderResp.getPayableAmount().toString(), placeOrderResp.getUserId());
            Long orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			//SELECT * FROM `orderrisk`.high_risk_seller limit 10;
			//SELECT * FROM `orderrisk`.high_risk_product limit 10;
			computeBean.setBizNo(TestDataManager.getRandomUUID().toString());
			computeBean.setBizType(11);
			Map<String, String> fact=new HashedMap();
			fact.put("orderid", orderid.toString());
			fact.put("SellerNewUserDayCount", "20");
			computeBean.setFact(fact);
			computeCall.setData(computeBean);
			computeCall.callService();			
			
			Logger.verifyEquals("true", computeCall.getString("success"), "验证success");
			RuleComputeResp data = computeCall.getData();
			Logger.verifyEquals(true, data.getHit(), "getHit");
			Logger.verifyEquals(33, data.getHitRuleId(), "getHitRuleId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_013() {
		Logger.start(true,"同一店铺一天内黄牛用户下单数量>=20风控-黄牛用户");
		try {
			PlaceOrderReq placeOrderReq = TradingCallService.buildDemoReq(false);

			OrderVo orderVo = placeOrderReq.getOrderGroups().get(0).getOrders()
					.get(0);
			OrderItemVo orderItemVo = orderVo.getOrderItems().get(0);
			orderItemVo.setFreight(new BigDecimal(3));
			
			Logger.comment("############################下单-支付-卖家接单-鉴定中心发货-确认收货");
			//下单-支付-卖家接单-鉴定中心发货-确认收货
            TradingServiceResp placeOrderResp = TradingCallService.callPlaceOrderService(placeOrderReq);

            String payType = "13";

            // 支付收单
            Long thirdTradingId = TradingCallService.payOrder(placeOrderResp.getUserId(), BigDecimal.ZERO,
                            placeOrderResp.getTradingId(), payType);

            // 支付网关支付回调交易(付款)
            PayGateWayCallService.notifyCall(payType, String.valueOf(thirdTradingId),
                            placeOrderResp.getPayableAmount().toString(), placeOrderResp.getUserId());
            Long orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			//SELECT * FROM `orderrisk`.high_risk_seller limit 10;
			//SELECT * FROM `orderrisk`.high_risk_product limit 10;
			computeBean.setBizNo(TestDataManager.getRandomUUID().toString());
			computeBean.setBizType(11);
			Map<String, String> fact=new HashedMap();
			fact.put("orderid", orderid.toString());
			fact.put("SellerBadPersonDayCount", "20");
			computeBean.setFact(fact);
			computeCall.setData(computeBean);
			computeCall.callService();			
			
			Logger.verifyEquals("true", computeCall.getString("success"), "验证success");
			RuleComputeResp data = computeCall.getData();
			Logger.verifyEquals(true, data.getHit(), "getHit");
			Logger.verifyEquals(34, data.getHitRuleId(), "getHitRuleId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_014() {
		Logger.start(true,"同一店铺一天内半年未登录用户下单数量>=20，且历史无付款订单风控-历史未登录用户");
		try {
			PlaceOrderReq placeOrderReq = TradingCallService.buildDemoReq(false);

			OrderVo orderVo = placeOrderReq.getOrderGroups().get(0).getOrders()
					.get(0);
			OrderItemVo orderItemVo = orderVo.getOrderItems().get(0);
			orderItemVo.setFreight(new BigDecimal(3));
			
			Logger.comment("############################下单-支付-卖家接单-鉴定中心发货-确认收货");
			//下单-支付-卖家接单-鉴定中心发货-确认收货
            TradingServiceResp placeOrderResp = TradingCallService.callPlaceOrderService(placeOrderReq);

            String payType = "13";

            // 支付收单
            Long thirdTradingId = TradingCallService.payOrder(placeOrderResp.getUserId(), BigDecimal.ZERO,
                            placeOrderResp.getTradingId(), payType);

            // 支付网关支付回调交易(付款)
            PayGateWayCallService.notifyCall(payType, String.valueOf(thirdTradingId),
                            placeOrderResp.getPayableAmount().toString(), placeOrderResp.getUserId());
            Long orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			//SELECT * FROM `orderrisk`.high_risk_seller limit 10;
			//SELECT * FROM `orderrisk`.high_risk_product limit 10;
			computeBean.setBizNo(TestDataManager.getRandomUUID().toString());
			computeBean.setBizType(11);
			Map<String, String> fact=new HashedMap();
			fact.put("orderid", orderid.toString());
			fact.put("SellerHalfYearUnLoginDayCount", "20");
			computeBean.setFact(fact);
			computeCall.setData(computeBean);
			computeCall.callService();			
			
			Logger.verifyEquals("true", computeCall.getString("success"), "验证success");
			RuleComputeResp data = computeCall.getData();
			Logger.verifyEquals(true, data.getHit(), "getHit");
			Logger.verifyEquals(35, data.getHitRuleId(), "getHitRuleId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Compute_015() {
		Logger.start(true,"同一店铺一天内一年未登录用户下单数量>=20，且历史无付款订单");
		try {
			PlaceOrderReq placeOrderReq = TradingCallService.buildDemoReq(false);

			OrderVo orderVo = placeOrderReq.getOrderGroups().get(0).getOrders()
					.get(0);
			OrderItemVo orderItemVo = orderVo.getOrderItems().get(0);
			orderItemVo.setFreight(new BigDecimal(3));
			
			Logger.comment("############################下单-支付-卖家接单-鉴定中心发货-确认收货");
			//下单-支付-卖家接单-鉴定中心发货-确认收货
            TradingServiceResp placeOrderResp = TradingCallService.callPlaceOrderService(placeOrderReq);

            String payType = "13";

            // 支付收单
            Long thirdTradingId = TradingCallService.payOrder(placeOrderResp.getUserId(), BigDecimal.ZERO,
                            placeOrderResp.getTradingId(), payType);

            // 支付网关支付回调交易(付款)
            PayGateWayCallService.notifyCall(payType, String.valueOf(thirdTradingId),
                            placeOrderResp.getPayableAmount().toString(), placeOrderResp.getUserId());
            Long orderid = placeOrderResp.getOrderList().get(0).getOrderId();
			//SELECT * FROM `orderrisk`.high_risk_seller limit 10;
			//SELECT * FROM `orderrisk`.high_risk_product limit 10;
			computeBean.setBizNo(TestDataManager.getRandomUUID().toString());
			computeBean.setBizType(11);
			Map<String, String> fact=new HashedMap();
			fact.put("orderid", orderid.toString());
			fact.put("SellerOneYearUnLoginDayCount", "20");
			computeBean.setFact(fact);
			computeCall.setData(computeBean);
			computeCall.callService();			
			
			Logger.verifyEquals("true", computeCall.getString("success"), "验证success");
			RuleComputeResp data = computeCall.getData();
			Logger.verifyEquals(true, data.getHit(), "getHit");
			Logger.verifyEquals(36, data.getHitRuleId(), "getHitRuleId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}